cmake_minimum_required(VERSION 3.16)

project(UserManagement VERSION 0.1 LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets LinguistTools sql)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets LinguistTools sql)



set(PROJECT_SOURCES
  main.cpp
  Widget.cpp
  Widget.h

  #将翻译文件添加进项目源中
  ${TS_FILES}
)

#添加子窗口，登录窗口
add_subdirectory(GUIWidget/LoginWidget)
add_subdirectory(GUIWidget/BaseWidgets)
add_subdirectory(GUIWidget/PatientManagerWidget)
add_subdirectory(ServiceManager/LoginService)
add_subdirectory(ServiceManager/LoadPatientList)
add_subdirectory(DataManager/UserRepository)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
  qt_add_executable(UserManagement
    MANUAL_FINALIZATION
    ${PROJECT_SOURCES}
  )
#根据代码中的tr命令生成TS文件的命令
qt_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})


target_link_libraries(UserManagement PUBLIC
  Qt${QT_VERSION_MAJOR}::Widgets
  Qt6::Sql
  BaseWidgets
  LoginWidget
  PatientManagerWidget
  UserRepository
  LoadPatientList


)

set_target_properties(UserManagement PROPERTIES
  ${BUNDLE_ID_OPTION}
  MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
  MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
  MACOSX_BUNDLE TRUE
  WIN32_EXECUTABLE TRUE
)

endif()

#将子项目的src文件路径加载进来,并放在header_list变量下
file(GLOB_RECURSE  header_list
  "${CMAKE_SOURCE_DIR}/GUIWidget/BaseWidgets/*.h"
  "${CMAKE_SOURCE_DIR}/GUIWidget/LoginWidget/*.h"
)

file(GLOB_RECURSE  source_list
  "${CMAKE_SOURCE_DIR}/GUIWidget/BaseWidgets/*.cpp"
  "${CMAKE_SOURCE_DIR}/GUIWidget/LoginWidget/*.cpp"
)

message(STATUS "source_list contents:")
foreach(src ${source_list})
    message(STATUS "  ${src}")
endforeach()






#设置tr需要翻译的头文件和源文件
set(tr_source_list  ${source_list} ${header_list} ${PROJECT_SOURCES} )
#确保qm_files目录存在
set(qm_output_path ${CMAKE_SOURCE_DIR}/Translations/qm_files)
file(MAKE_DIRECTORY ${qm_output_path})  # 保证目录存在

#使用lupdate工具从源文件生成ts文件,放在TS_FILES下
qt_add_lupdate(${PROJECT_NAME} TS_FILES

  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_zh_CN.ts #中文
  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_en_US.ts #英语
  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_ko_KR.ts #韩语
  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_ja_JP.ts #日语
  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_es_ES.ts #
  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_fr_FR.ts #法语
  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_el_GR.ts #希腊语
  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_it_IT.ts #意大利语
  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_pl_PL.ts #波兰语

  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_pt_PT.ts #葡萄牙语
  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_ro_RO.ts #罗马
  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_ru_RU.ts #俄罗斯
  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_tr_TR.ts #土耳其
  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_cs_CZ.ts #捷克
  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_de_DE.ts #德语
  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_hu_HU.ts #
  SOURCES ${tr_source_list} #添加翻译te针对的的源文件
)

add_custom_target(update_all ALL DEPENDS ${PROJECT_NAME}_lupdate)



qt_add_lrelease(${PROJECT_NAME} TS_FILES

  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_zh_CN.ts #中文
  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_en_US.ts #英语
  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_ko_KR.ts #韩语
  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_ja_JP.ts #日语
  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_es_ES.ts #
  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_fr_FR.ts #法语
  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_el_GR.ts #希腊语
  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_it_IT.ts #意大利语
  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_pl_PL.ts #波兰语

  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_pt_PT.ts #葡萄牙语
  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_ro_RO.ts #罗马
  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_ru_RU.ts #俄罗斯
  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_tr_TR.ts #土耳其
  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_cs_CZ.ts #捷克
  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_de_DE.ts #德语
  ${CMAKE_SOURCE_DIR}/Translations/ts_files/${PROJECT_NAME}_hu_HU.ts #

  #将QM文件生成在该路径下

  QM_FILES_OUTPUT_VARIABLE qm_files
  OUTPUT_DIRECTORY ${qm_output_path}
)
# 确保构建顺序：先lupdate后lrelease
add_dependencies(${PROJECT_NAME}_lrelease ${PROJECT_NAME}_lupdate)

#最后需要将qm文件拷贝至build目录下

include(GNUInstallDirs)
install(TARGETS UserManagement
  BUNDLE DESTINATION .
  LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

#将res资源文件路径复制到可执行文件路径下
# 假设你的可执行文件叫 my_app


# 构建后复制 assets 目录到目标目录
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
        ${CMAKE_SOURCE_DIR}/res
        $<TARGET_FILE_DIR:${PROJECT_NAME}>/res
)


if(QT_VERSION_MAJOR EQUAL 6)
  qt_finalize_executable(UserManagement)
endif()






